package org.springsocial.dao.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springsocial.dao.TweetDao;
import org.springsocial.domain.ExtendedTweet;

@Repository
public class TweetDaoImpl extends AbstractJpaDao<ExtendedTweet> implements TweetDao {
	public TweetDaoImpl() {
		setClazz(ExtendedTweet.class);
	}
	@Override
	public ExtendedTweet saveTweet(ExtendedTweet tweet) {
		 super.save(tweet);
		 return tweet;
	}
	@Override
	public ExtendedTweet getTweetById(Long id) {
		return super.findOne(id);
	}
	@Override
	public List<ExtendedTweet> getTweetsFromToDate(Date from, Date to) {
		TypedQuery<ExtendedTweet> query = entityManager
				.createQuery(
						"select distinct t from ExtendedTweet t where t.createdAt between :from and :to",
						ExtendedTweet.class);
		query.setParameter("from", from, TemporalType.DATE);
		query.setParameter("to", to, TemporalType.DATE);
		return query.getResultList();
	}

}
